<template>
  <div class="app">
    <div class="tabs">
      <template v-for="(item, index) in tabs" :key="index">
        <button :class="{ active: index === curentIndex }" @click="itemClick(index)">{{ item }}</button>
      </template>
    </div>

    <div class="view">
      <!--以逗号分隔,且不能有空格-->
      <!--include: 组件的名称来自于组件定义时name的选项-->
      <keep-alive include="home,about"> 
        <component :is="tabs[curentIndex]"></component>
      </keep-alive>
    </div>
  </div>
</template>

<script>
  import Home from './views/Home.vue'
  import About from './views/About.vue'
  import Category from './views/Category.vue'

  export default {
    data() {
      return {
        tabs: ['home', 'about', 'category'],
        curentIndex: 0
      }
    },
    methods: {
      itemClick(index) {
        this.curentIndex = index
      }
    },
    components: {
      Home,
      About,
      Category
    }
  }
</script>

<style scoped>
  .active {
    color: red;
  }
</style>